//    Copyright (C) Kherty.  All rights reserved.
#region

using System;
using System.Windows.Media.Imaging;
using OpenLS.Core.Serialization;
using OpenLS.Vml.Serialization;

#endregion

namespace OpenLS.Vml
{
    public class Fill : ShapeElement
    {
        private const string ImageRelationshipNamespace =
            @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";

        internal const string xmlName = "fill";
        private string _alignshape;
        private string _althref;
        private double? _angle;
        private ImageAspect? _aspect;
        private string _color;
        private string _color2;
        private string _colors;
        private string _detectmouseclick;


        private string _ext;
        private string _focus;
        private string _focusposition;
        private string _focussize;
        private string _href;
        private VmlImage _img;
        private FillMethod? _method;
        private string _on;
        private string _opacity;
        private string _opacity2;
        private string _origin;
        private string _position;
        private string _recolor;
        private string _rotate;
        private string _size;
        private string _src;
        private string _title;


        private FillType? _type;
        private Drawing.Fill _fill;

        public BitmapSource Image
        {
            get
            {
                if (_img != null)
                    return _img.Source;
                return null;
            }
            set
            {
                if (value == null)
                    _img = null;
                else
                    _img = new VmlImage(value);
            }
        }

        public string Ext
        {
            get { return _ext; }
            set { _ext = value; }
        }

        public FillType? Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public string On
        {
            get { return _on; }
            set { _on = value; }
        }

        public string Color
        {
            get { return _color; }
            set { _color = value; }
        }

        public string Opacity
        {
            get { return _opacity; }
            set { _opacity = value; }
        }

        public string Color2
        {
            get { return _color2; }
            set { _color2 = value; }
        }

        public string Source
        {
            get { return _src; }
            set { _src = value; }
        }

        public string HRef
        {
            get { return _href; }
            set { _href = value; }
        }

        public string AltHRef
        {
            get { return _althref; }
            set { _althref = value; }
        }

        public string Size
        {
            get { return _size; }
            set { _size = value; }
        }

        public string Origin
        {
            get { return _origin; }
            set { _origin = value; }
        }

        public string Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public ImageAspect? Aspect
        {
            get { return _aspect; }
            set { _aspect = value; }
        }

        public string Colors
        {
            get { return _colors; }
            set { _colors = value; }
        }

        public double? Angle
        {
            get { return _angle; }
            set { _angle = value; }
        }

        public string AlignShape
        {
            get { return _alignshape; }
            set { _alignshape = value; }
        }

        public string Focus
        {
            get { return _focus; }
            set { _focus = value; }
        }

        public string FocusSize
        {
            get { return _focussize; }
            set { _focussize = value; }
        }

        public string FocusPosition
        {
            get { return _focusposition; }
            set { _focusposition = value; }
        }

        public FillMethod? Method
        {
            get { return _method; }
            set { _method = value; }
        }

        public string DetectMouseClick
        {
            get { return _detectmouseclick; }
            set { _detectmouseclick = value; }
        }

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public string Opacity2
        {
            get { return _opacity2; }
            set { _opacity2 = value; }
        }

        public string ReColor
        {
            get { return _recolor; }
            set { _recolor = value; }
        }

        public string Rotate
        {
            get { return _rotate; }
            set { _rotate = value; }
        }

       
        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read(VmlConstants.VmlNamespace + xmlName))
            {
                Type = c.GetOptionalEnum<FillType>("type");
                On = c.GetOptionalString("on");
                Color = c.GetOptionalString("color");
                Opacity = c.GetOptionalString("opacity");
                Color2 = c.GetOptionalString("color2");
                Source = c.GetOptionalString("src");
                HRef = c.GetOptionalString(VmlConstants.OfficeNamespace + "href");
                AltHRef = c.GetOptionalString("althref");
                Size = c.GetOptionalString("size");
                Origin = c.GetOptionalString("origin");
                Position = c.GetOptionalString("position");
                Aspect = c.GetOptionalEnum<ImageAspect>("aspect");
                Colors = c.GetOptionalString("colors");
                Angle = c.GetOptionalDouble("angle");
                AlignShape = c.GetOptionalString("alignshape");
                Focus = c.GetOptionalString("focus");
                FocusSize = c.GetOptionalString("focussize");
                FocusPosition = c.GetOptionalString("focusposition");
                Method = c.GetOptionalEnum<FillMethod>("method");
                DetectMouseClick = c.GetOptionalString(VmlConstants.OfficeNamespace + "detectmouseclick");
                Title = c.GetOptionalString(VmlConstants.OfficeNamespace + "title");
                Opacity2 = c.GetOptionalString(VmlConstants.OfficeNamespace + "opacity2");
                ReColor = c.GetOptionalString("recolor");
                Rotate = c.GetOptionalString("rotate");
                var Rid = c.GetOptionalString(OfficeConstants.RelationshipNamespace + "id");
                if (Rid != null)
                {
                    loadImage(c, Rid);
                }


                string relId = c.GetOptionalString(VmlConstants.OfficeNamespace + "relid");
                if (relId != null)
                {
                    loadImage(c, relId);
                }
                _fill = c.ReadOptionalElement<OpenLS.Vml.Drawing.Fill>("fill");
            }
        }

        private void loadImage(ReadContext c, string Rid)
        {
            _img = VmlImage.Load(c, Rid);
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (var c = context.Write(VmlConstants.VmlNamespace + xmlName))
            {
                c.SetOptionalEnum("type", Type);
                c.SetOptionalString("on", On);
                c.SetOptionalString("color", Color);
                c.SetOptionalString("opacity", Opacity);
                c.SetOptionalString("color2", Color2);
                c.SetOptionalString("src", Source);
                c.SetOptionalString(VmlConstants.OfficeNamespace + "href", HRef);
                c.SetOptionalString(VmlConstants.OfficeNamespace + "althref", AltHRef);
                c.SetOptionalString("size", Size);
                c.SetOptionalString("origin", Origin);
                c.SetOptionalString("position", Position);
                c.SetOptionalEnum("aspect", Aspect);
                c.SetOptionalString("colors", Colors);
                c.SetOptionalDouble("angle", Angle);
                c.SetOptionalString("alignshape", AlignShape);
                c.SetOptionalString("focus", Focus);
                c.SetOptionalString("focussize", FocusSize);
                c.SetOptionalString("focusposition", FocusPosition);
                c.SetOptionalEnum("method", Method);
                c.SetOptionalString(VmlConstants.OfficeNamespace + "detectmouseclick", DetectMouseClick);
                c.SetOptionalString(VmlConstants.OfficeNamespace + "title", Title);
                c.SetOptionalString(VmlConstants.OfficeNamespace + "opacity2", Opacity2);
                c.SetOptionalString("recolor", ReColor);
                c.SetOptionalString("rotate", Rotate);
                if (_img != null)
                {
                    _img.Write(c, "background");
                }
               // c.SetOptionalString(VmlConstants.OfficeNamespace + "relid", RelId);
                //if (RelId != null)
                  //  throw new NotImplementedException();
                c.WriteOptionalElement(_fill);
            }
        }
    }
}